<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <a href="#/zhuye">主页</a>
        <a href="#/keji">科技</a>
        <a href="#/caijing">财经</a>
        <a href="#/yule">娱乐</a>

        <!-- 根据 :is 属性指定组件名称，把对应的组件渲染到component标签所在位置 -->
        <!-- 可以把component标签当作是 [组件的占位符] -->
        <component :is="comName"></component>
    </div>
    <script>
        // #region定义需要被切的4个组件
        const zhuye = {
            template: '<h1>主页信息</h1>'
        }
        const keji = {
            template: '<h1>科技信息</h1>'
        }
        const caijing = {
            template: '<h1>财经信息</h1>'
        }
        const yule = {
            template: '<h1>娱乐信息</h1>'
        }

        // #region vue实例对象
        const m = new Vue({
            el: '#app',
            data: {
                comName: 'zhuye'
            },
            components: {
                // 注册私有组件
                zhuye,
                keji,
                caijing,
                yule
            }
        })


        // 监听window的onhashchange事件，根据获取到最新hash值切换要显示的组件内容
        window.onhashchange = function() {
            // 通过lacation.hash()获取最新hash值
            console.log(location.hash);
            switch (location.hash.slice(1)) {
                case '/zhuye':
                    m.comName = 'zhuye'
                    break
                case '/keji':
                    m.comName = 'keji'
                    break
                case '/caijing':
                    m.comName = 'caijing'
                    break
                case '/yule':
                    m.comName = 'yule'
                    break
            }

        }
    </script>

</body>

</html>